package com.example.gokchinesefoodmappcdev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.gokchinesefoodmappcdev.dao.MarketingCommodityDao;
import com.example.gokchinesefoodmappcdev.dao.MarketingPictureDao;
import com.example.gokchinesefoodmappcdev.entity.MarketingCommodity;
import com.example.gokchinesefoodmappcdev.entity.MarketingPicture;
import com.example.gokchinesefoodmappcdev.entity.dto.MarketingDTO;
import com.example.gokchinesefoodmappcdev.service.MarketingService;
import com.example.gokchinesefoodmappcdev.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MarketingServiceImpl implements MarketingService {
    /**
     * 新增首页运营管理接口
     * @param marketingDTO
     * @return
     */
    @Autowired
    private MarketingPictureDao marketingPictureDao;
    @Autowired
    private MarketingCommodityDao marketingCommodityDao;

    /**
     *新增营销商品信息
     * @param marketingDTO
     * @return
     */
    @Override
    public ResultData insertMarketingCommodityAndMarketingPicture(MarketingDTO marketingDTO) {
        // 添加图片
        List<MarketingPicture> pictureList = marketingDTO.getPictureList();
        for (MarketingPicture picture : pictureList) {
            marketingPictureDao.insert(picture);
        }
        // 添加商品信息
        List<MarketingCommodity> commodityList = marketingDTO.getCommodityList();
        for (MarketingCommodity commodity : commodityList) {
            marketingCommodityDao.insert(commodity);
        }
        return ResultData.ok(ResultData.OPERATION_SUCCESS,"添加成功");
    }

    /**
     * 营销商品列表查看
     * @return
     */
    @Override
    public ResultData selectMarketing() {
        // 查询图片
        QueryWrapper<MarketingPicture> pictureWrapper = new QueryWrapper();
        List<MarketingPicture> pictureList = marketingPictureDao.selectList(pictureWrapper);
        // 查询商品信息
        QueryWrapper<MarketingCommodity> commodityWrapperwrapper = new QueryWrapper();
        List<MarketingCommodity> commodityList = marketingCommodityDao.selectList(commodityWrapperwrapper);
        MarketingDTO marketingDTO = new MarketingDTO();
        marketingDTO.setCommodityList(commodityList);
        marketingDTO.setPictureList(pictureList);
        return ResultData.createData(ResultData.OPERATION_SUCCESS,marketingDTO);
    }

    /**
     * 删除首页运行管理图片
     * @param id
     * @return
     */
    @Override
    public ResultData deleteMarketingPictureById(Integer id) {
        QueryWrapper<MarketingPicture> pictureWrapper = new QueryWrapper();
        pictureWrapper.eq("id",id);
        int i = marketingPictureDao.delete(pictureWrapper);
        if (i > 0) {
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"删除成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL,"删除失败");
    }

    /**
     * 删除营销商品
     * @param id
     * @return
     */
    @Override
    public ResultData deleteMarketingCommodById(Integer id) {
        QueryWrapper<MarketingCommodity> commodityWrapper = new QueryWrapper();
        commodityWrapper.eq("id",id);
        int i = marketingCommodityDao.delete(commodityWrapper);
        if (i > 0) {
            return ResultData.ok(ResultData.OPERATION_SUCCESS,"删除成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL,"删除失败");
    }
}
